package com.qg.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.qg.dto.ReturnResult;
import com.qg.dto.ReturnResultUtils;
import com.qg.pojo.QgGoods;
import com.qg.pojo.QgGoodsMessage;
import com.qg.pojo.QgGoodsTempStock;
import com.qg.pojo.QgUser;
import com.qg.service.LocalGoodsService;
import com.qg.service.QgGoodsMessageService;
import com.qg.service.QgGoodsService;
import com.qg.service.QgGoodsTempStockService;
import com.qg.util.EmptyUtils;
import com.qg.util.IdWorker;
import com.qg.util.RedisUtil;
import com.qg.vo.GoodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.qg.util.Canstants;

import java.awt.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LocalGoodsServiceImpl implements LocalGoodsService {

    @Reference
    private QgGoodsService qgGoodsService;

    @Reference
    private QgGoodsTempStockService qgGoodsTempStockService;
    @Reference
    private QgGoodsMessageService qgGoodsMessageService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public GoodsVo queryGoodsById(String id) throws Exception {
        GoodsVo goodsVo=new GoodsVo();;
        String goodsVoJson=redisUtil.getStr(Canstants.goods+id);
        if(EmptyUtils.isEmpty(goodsVoJson)){
            //获得商品信息
            QgGoods qgGoods=qgGoodsService.getQgGoodsById(id);
            //获得商品实际库存
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("goodsId",id);
            param.put("status",0);
            Integer Count=qgGoodsTempStockService.getQgGoodsTempStockCountByMap(param);
            BeanUtils.copyProperties(qgGoods,goodsVo);
            goodsVo.setTempStock(qgGoods.getStock()-Count);
            //放在redis当中
            redisUtil.setStr(Canstants.goods+goodsVo.getId(), JSONObject.toJSONString(goodsVo));
        }else{
            goodsVo=JSONObject.parseObject(goodsVoJson,GoodsVo.class);
        }
        return goodsVo;
    }

    @Override
    public ReturnResult getGoods(String token, String goodsId) throws Exception {
        //验证用户登录状态
        String userJson=redisUtil.getStr(token);
        QgUser qgUser=JSONObject.parseObject(userJson, QgUser.class);
        if(EmptyUtils.isNotEmpty(qgUser)){
            //2.判断用户是否已经抢购商品
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("goodsId",goodsId);
            param.put("status",1);
            param.put("userId",qgUser.getId());
            List<QgGoodsTempStock> list=qgGoodsTempStockService.getQgGoodsTempStockListByMap(param);
            if(EmptyUtils.isNotEmpty(list)){
                return ReturnResultUtils.returnFail(2002,"用户已抢购一次");
            }
            String goodsJSON=redisUtil.getStr(Canstants.goods+goodsId);
            GoodsVo goodsVo=JSONObject.parseObject(goodsJSON,GoodsVo.class);
            //判断库存是否充足
            if (goodsVo.getTempStock()<=0){
                return ReturnResultUtils.returnFail(2003,"商品库存不足");
            }
            //更新临时库存表数据
            QgGoodsTempStock qgGoodsTempStock=new QgGoodsTempStock();
            qgGoodsTempStock.setId(IdWorker.getId());
            qgGoodsTempStock.setGoodsId(goodsId);
            qgGoodsTempStock.setUserId(qgUser.getId());
            qgGoodsTempStock.setStatus(1);
            qgGoodsTempStock.setCreatedTime(new Date());
            qgGoodsTempStock.setUpdatedTime(null);
            qgGoodsTempStockService.qdtxAddQgGoodsTempStock(qgGoodsTempStock);
            goodsVo.setTempStock(goodsVo.getTempStock()-1);
            String newJson=JSONObject.toJSONString(goodsVo);
            redisUtil.setStr(Canstants.goods+goodsId,newJson);
        }
        return ReturnResultUtils.resultSuccess(2000,"商品抢购成功");
    }
}
